package jms;

import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageConsumer;
import javax.jms.Session;
import javax.jms.TextMessage;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;

public class Consumer {
	public static void main(String[] args) throws NamingException, JMSException {
		Context context=new InitialContext();
		ConnectionFactory connectionFactory=(ConnectionFactory) context.lookup("ConnectionFactory");
		Destination destination=(Destination) context.lookup(Producer.desName);//"topic/.."
		
		Connection connection=connectionFactory.createConnection();
//		Session session=connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
		Session session=connection.createSession(false, Session.CLIENT_ACKNOWLEDGE);//msg.acknowledge();
		MessageConsumer consumer=session.createConsumer(destination);
		connection.start();//通知目标可以分发消息
		Message msg=consumer.receive();
		msg.acknowledge();
		if(msg instanceof TextMessage){
			TextMessage message=(TextMessage) msg;
			System.out.println(message.getText());
		}
		connection.close();
	}
}
